﻿@{
    ViewBag.Title = "Документы";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<header class="jumbotron subhead" id="overview">
    <h1>Входящие накладные</h1>
    <p class="lead">Просмотр информации по входящим накладным.</p>
</header>

<div class="input-append">
    <input type="text" class="input-xxlarge" name="FILTER" value="$top=100" />
    <button class="btn" type="button" data-action="filter"><i class="icon-filter"></i> Фильтр</button>
</div>

<table data-name="INVOICES" class="table table-striped">
    <thead>
        <tr>
            <th data-name="COMPANY.COMPANY_NAME">
                Поставщик
            </th>
            <th data-name="CUSTOMER.COMPANY_NAME">
                Покупатель
            </th>
            <th data-name="LOCATION.LOCATION_NAME">
                Склад
            </th>
            <th data-name="DOC_DATE">
                Дата документа
            </th>
            <th data-name="DOC_NUMBER">
                № документа
            </th>
            <th data-name="TOTAL_AMOUNT">
                Сумма
            </th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

<p>
    <a href="#" class="btn" data-action="download"> Загрузить отмеченные накладные</a>
</p>

<div class="modal hide" data-name="INVOICES">

    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h3>Редактирование документа</h3>
    </div>

    <div class="modal-body">

        <form name="INVOICES" class="form-horizontal" method="post"> 
            <fieldset> 

                <input type="hidden" name="GUID_RECORD" value="" />
                <input type="hidden" name="COMPANY_GUID" value="" />
                <input type="hidden" name="CUSTOMER_GUID" value="" />


                <div class="control-group"> 
                    <label class="control-label" for="COMPANY_GUID">Компания отправитель</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="COMPANY.COMPANY_NAME" value="" readonly="readonly">
                    </div> 
                </div> 

                <div class="control-group"> 
                    <label class="control-label" for="CUSTOMER_GUID">Компания получатель</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="CUSTOMER.COMPANY_NAME" value="" readonly="readonly">
                    </div> 
                </div> 

                <div class="control-group"> 
                    <label class="control-label" for="LOCATION_GUID">Склад</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="LOCATION.LOCATION_NAME" value="" readonly="readonly">
                    </div> 
                </div> 

                <div class="control-group"> 
                    <label class="control-label" for="DOC_DATE">Дата докумена</label>
                    <div class="controls">
                        <div id="dt1" class="input-append date" data-date="@DateTime.Now.ToString("mm.dd.yyyy")" data-date-format="mm.dd.yyyy">
                            <input type="text" class="input-xlarge" name="DOC_DATE" value="" readonly="readonly">
                            <span class="add-on"><i class="icon-th"></i></span>
                        </div>
                    </div> 
                </div> 

                <div class="control-group"> 
                    <label class="control-label" for="DOC_NUMBER">№ докумена</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="DOC_NUMBER" value="">
                    </div> 
                </div> 
                
                <div class="control-group"> 
                    <label class="control-label" for="TOTAL_AMOUNT">Итоговая сумма</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" readonly="readonly" name="TOTAL_AMOUNT" value="">
                    </div>
                </div> 
                
                <div class="control-group"> 
                    <label class="control-label" for="TAX_AMOUNT">Сумма налога</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" readonly="readonly" name="TAX_AMOUNT" value="">
                    </div>
                </div> 
                
                <div class="control-group"> 
                    <label class="control-label" for="NOTE">Примечание</label>
                    <div class="controls">
                        <textarea class="input-xlarge" name="NOTE"></textarea>
                    </div>
                </div> 

            </fieldset>
        </form>

        <table data-name="INVOICE_LINES" class="table table-striped">
            <thead>
                <tr>
                    <th data-name="ITEM.ITEM_NAME">
                        Товар
                    </th>
                    <th data-name="ITEM.EXT_KEY">
                        Код товара
                    </th>
                    <th data-name="QUANTITY">
                        Количество
                    </th>
                    <th data-name="PRICE">
                        Цена
                    </th>
                    <th data-name="TOTAL">
                        Сумма
                    </th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>

    </div>

    <div class="modal-footer">
        <a href="#" class="btn" data-dismiss="modal">Закрыть</a>
    </div>
</div>



<div class="modal hide" data-name="INVOICE_LINES">

    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h3>Редактирование позиции</h3>
    </div>

    <div class="modal-body">

        <form name="INVOICE_LINES" class="form-horizontal" method="post"> 
            <fieldset> 

                <input type="hidden" data-name="GUID_RECORD" name="GUID_RECORD" value="" />
                <input type="hidden" name="INVOICE_GUID" value="" />

                <div class="control-group"> 
                    <label class="control-label" for="ITEM_GUID">Товар</label>
                    <div class="controls">
                        <div class="input-append">
                            <input type="text" class="input-xlarge" name="ITEM.ITEM_NAME" value="" readonly="readonly">
                            <a href="#" class="btn" data-action="EDIT-ITEM-RECODE"><i class="icon-edit"></i></a>
                        </div>
                    </div> 
                </div> 

                <div class="control-group"> 
                    <label class="control-label" for="QUANTITY">Количество</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="QUANTITY" value="">
                    </div> 
                </div> 

                <div class="control-group"> 
                    <label class="control-label" for="PRICE">Цена</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="PRICE" value="">
                    </div> 
                </div> 

                <div class="control-group"> 
                    <label class="control-label" for="TOTAL">Сумма</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="TOTAL" value="">
                    </div> 
                </div> 

            </fieldset>
        </form>
        
    </div>

    <div class="modal-footer">
        <a href="#" class="btn" data-dismiss="modal">Закрыть</a>
    </div>
</div>

<div class="modal hide" data-name="ITEMS_MAPPING">

    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h3>Установить соответствие</h3>
    </div>

    <div class="modal-body">

        <form name="ITEMS_MAPPING" class="form-horizontal" method="post"> 
            <fieldset> 
                
                <input type="hidden" name="SUPPLIER_ITEM_GUID" value="">

                <div class="control-group"> 
                    <label class="control-label" for="COMPANY_NAME">Компания</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="COMPANY_NAME" value="" readonly="readonly">
                    </div> 
                </div> 
                
                <div class="control-group"> 
                    <label class="control-label" for="ITEM_NAME">Наименование товара</label>
                    <div class="controls">
                        <input type="text" class="input-xlarge" name="ITEM_NAME" value="" readonly="readonly">
                    </div> 
                </div> 
                
                <div class="control-group">
                    <div class="controls">
                        <div class="input-append">
                            <input type="text" class="input-medium" name="ITEM-FILTER" value="" />
                            <button class="btn" type="button" data-action="ITEM-FILTER"><i class="icon-filter"></i></button>
                        </div>
                    </div>
                </div>

                <div class="control-group"> 
                    <label class="control-label" for="ITEM_GUID">Наименование товара</label>
                    <div class="controls">
                        <select 
                            data-value="GUID_RECORD" 
                            data-text="ITEM_NAME" name="ITEM_GUID"></select>
                    </div> 
                </div> 

            </fieldset>
        </form>
        
    </div>

    <div class="modal-footer">
        <a href="#" class="btn" data-dismiss="modal">Закрыть</a>
        <a href="#" class="btn btn-primary" data-action="ITEMS_MAPPING-save">Сохранить</a>
    </div>
</div>


<script type="text/javascript">

    $(function () {

        var office = new Office();

        $.get("/edex/api/INVOICES/GetIncoming/", function (data) {

            office.Store["INVOICES"] = data;

            office.Grid({
                tableName: "INVOICES",
                data: data
            });

            $("table[data-name='INVOICES'] tr").each(function (i, r) {

                var id = $(r).attr("data-id");

                if (id) {

                    var td = $("<td>");
                    td.attr("data-name", "SERVICE_COLUMN");

                    var check = $("<input>");
                    check.attr("type", "checkbox");
                    check.attr("name", "GUID_RECORDS");
                    check.attr("data-name", "SELECTED_RECORDS");
                    check.val(id);

                    $(td).append(check);
                    $(r).append(td);
                }
            });
        });

        //$("#dt1").datepicker();

        $("select[data-autoload='true']").each(function () {
            
            var element = $(this);
            var name = $(this).attr("data-name");
            var data_value = $(this).attr("data-value");
            var data_text = $(this).attr("data-text");

            element.empty();

            $.get("/edex/api/" + name + "/", function (data) {

                office.Store[element.attr("name")] = data;

                $.each(data, function (index, itemData) {

                    var option = $("<option>");
                    option.val(itemData[data_value]);
                    option.text(itemData[data_text]);
                    
                    element.append(option);
                });
            });
        });

        $("table[data-name='INVOICES'] tr td").live("click", function () {

            if ($(this).attr("data-name") == "SERVICE_COLUMN")
                return;

            var id = $(this).parent().attr("data-id");

            $(office.Store["INVOICES"]).filter(function (i, r) {
                if (r["GUID_RECORD"] == id)
                    return true;
            }).each(function (i, r) {

                office.Store["INVOICE_LINES"] = r["LINES"];

                $('.modal[data-name="INVOICES"]').modal("show");

                office.Form.load({
                    renderTo: $('.modal[data-name="INVOICES"]'),
                    data: r
                });

                office.Form.load({
                    renderTo: $('.modal[data-name="INVOICE_LINES"]'),
                    data: r
                });

                office.Grid({
                    tableName: "INVOICE_LINES",
                    data: r["LINES"]
                });

            });
        });


        $("table[data-name='INVOICE_LINES'] tr td").live("click", function () {

            var id = $(this).parent().attr("data-id");

            $(office.Store["INVOICE_LINES"]).filter(function (i, r) {
                if (r["GUID_RECORD"] == id)
                    return true;
            }).each(function (i, r) {

                $('.modal[data-name="INVOICE_LINES"]').modal("show");

                office.Form.load({
                    renderTo: $('.modal[data-name="INVOICE_LINES"]'),
                    data: r
                });

            });
        });

        $(".btn[data-action='download']").on("click", function () {

            var ids = [];

            $("input[name='GUID_RECORDS']:checked").each(function (i, r) {
                ids.push[$(r).val()];
            });

            alert("Еще не работает");
        });

        $('.modal').on("show", function () {
            $(this).find("input").val("");
            $(this).find("select").val("");
            $(this).find("textarea").val("");
            $(this).find("tbody").empty();
        });


        $(".btn[data-action='filter']").on("click", function () {

            var filter = $("input[name='FILTER']").val();
            var uri = "/edex/api/INVOICES/GetIncoming/";

            if (filter != "")
                uri += "?" + filter;

            $.get(uri, function (data) {

                office.Grid({
                    tableName: "INVOICES",
                    data: data
                });
            });

        });


        $(".btn[data-action='EDIT-ITEM-RECODE']").on("click", function () {

            var id = $("input[data-name='GUID_RECORD']").val();
            var uri = "/edex/api/INVOICE_LINES/" + id;

            $.get(uri, function (data) {
                $('.modal[data-name="ITEMS_MAPPING"]').modal("show");

                $("input[name='SUPPLIER_ITEM_GUID']").val(data.ITEM.COMPANY.GUID_RECORD);
                $("input[name='COMPANY_NAME']").val(data.ITEM.COMPANY.COMPANY_NAME);
                $("input[name='ITEM_NAME']").val(data.ITEM.ITEM_NAME);
            });

        });

        


    });

</script>